﻿/*
* Created by SharpDevelop.
* User: v.i.evgenyev
* Date: 09.11.2011
* Time: 16:43
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Data.SQLite;
using System.Drawing;
using System.Windows.Forms;

namespace TinyCAT
{
/// <summary>
/// Description of Form1.
/// </summary>
	public partial class Form1 : Form
	{
		public static string[] findres;
		public static int curpos = 0;
		public Form1()
		{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
			InitializeComponent();

//
// TODO: Add constructor code after the InitializeComponent() call.
//
		}
//-------------form open-------------------------------------
		void Form1Shown(object sender, EventArgs e)
		{
			textBox1.Text = myAPP.textToTranslate.Value;
			findres = myAPP.findTMDB(textBox1.Text);
			if (myAPP.cntVar.Value > 0)
			{
//-----------если есть найденные значения в БД-------------
				textBox2.Text = findres[1];
				textBox2.BackColor = Color.LightYellow; //желтый цвет
				curpos =1;
			} else
			{
//-----------------если нет, то перевод из BING!------------
				textBox2.BackColor = Color.LightPink; //розовый цвет
				textBox2.Text = findres[0];
				curpos =0;
			}
		}
//------------finish translation fragment--------------------
		void Button1Click(object sender, EventArgs e)
		{
			myAPP.TranslatedText.Value = textBox2.Text;
			Close();
		}
//----- translated phrases through BING!---------------------
		void Button4Click(object sender, EventArgs e)
		{
			textBox2.Text = myAPP.BingTranslate(textBox1.Text,myAPP.setting.LangFrom, myAPP.setting.LangTo);
			textBox2.BackColor = Color.LightPink;
		}
//----Add translation in the translation memory database-----
		void Button5Click(object sender, EventArgs e)
		{
			try
			{
				SQLiteConnection cnn = new SQLiteConnection("Data Source = " + myAPP.setting.tmdbName);
				cnn.Open();
				SQLiteCommand SQLcommand = new SQLiteCommand(cnn);
				SQLiteParameter FromTxt = new SQLiteParameter("@FromTxt");
				SQLiteParameter ToTxt = new SQLiteParameter("@ToTxt");
				SQLiteParameter LFrom = new SQLiteParameter("@LFrom");
				SQLiteParameter LTo = new SQLiteParameter("@LTo");
				SQLiteParameter CNT = new SQLiteParameter("@CNT");
				SQLcommand.CommandText = "INSERT INTO Translate (FromTxt ,ToTxt ,FromLng ,ToLng ,CNT) VALUES(@FromTxt, @ToTxt, @LFrom, @LTo, @CNT)";
				SQLcommand.Parameters.Add(FromTxt);
				SQLcommand.Parameters.Add(ToTxt);
				SQLcommand.Parameters.Add(CNT);
				SQLcommand.Parameters.Add(LFrom);
				SQLcommand.Parameters.Add(LTo);
				FromTxt.Value = textBox1.Text;
				ToTxt.Value = textBox2.Text;
				CNT.Value = textBox1.Text.Length;
				LFrom.Value = myAPP.setting.LangFrom;
				LTo.Value = myAPP.setting.LangTo;
				SQLcommand.ExecuteNonQuery();
				textBox2.BackColor = Color.LightGreen;
				cnn.Close();
			}
			catch
			{
				MessageBox.Show("Database TM error.","TinyCAT",MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
		}
//-----------translated from the database--------------------
		void Button2Click(object sender, EventArgs e)
		{
			if(curpos > 0)
			{
				if ( curpos < myAPP.cntVar.Value)
				{
					curpos = curpos + 1;
					textBox2.Text = findres[curpos];
				} else
				{
					curpos = 1;
					textBox2.Text = findres[curpos];
				}
			}
		}
//-----------Update translation in the translation memory database--		
		void Button3Click(object sender, EventArgs e)
		{
			try
			{
				SQLiteConnection cnn = new SQLiteConnection("Data Source = " + myAPP.setting.tmdbName);
				cnn.Open();
				SQLiteCommand SQLcommand = new SQLiteCommand(cnn);
				SQLiteParameter FromTxt = new SQLiteParameter("@FromTxt");
				SQLiteParameter ToTxt = new SQLiteParameter("@ToTxt");
				SQLcommand.CommandText = "UPDATE Translate SET ToTxt = @ToTxt WHERE FromTxt = @FromTxt";
				SQLcommand.Parameters.Add(FromTxt);
				SQLcommand.Parameters.Add(ToTxt);
				FromTxt.Value = textBox1.Text;
				ToTxt.Value = textBox2.Text;
				SQLcommand.ExecuteNonQuery();
				textBox2.BackColor = Color.LightGreen;
				cnn.Close();
			}
			catch
			{
				MessageBox.Show("Database TM error.","TinyCAT",MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
		}
		
//-----------------------------------------------
		
	}
}
